package com.sailorj.controller;

import com.sailorj.entity.SysUser;
import com.sailorj.entity.TbStudent;
import com.sailorj.service.SysUserService;
import com.sailorj.service.TbStudentService;
import com.sailorj.util.NumUtil;
import com.sailorj.util.PageBean;
import com.sailorj.util.SysLoging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
* @Description:
* @Param:
* @return:
* @Author: huangshengjun
* @Date: 2020/4/19 19:29
*/
@CrossOrigin  //后端解决跨域方法注解
@Controller
@RequestMapping("/student") //访问的路径
public class TbStudentController {

    @Autowired
    private TbStudentService tbStudentService;

    @Autowired
    private SysUserService sysUserService;

    @ResponseBody
    @SysLoging("添加学生信息") //根据SysLoging的注解信息，进行方法的注释
    @RequestMapping(value = "/addStudent",method = RequestMethod.POST) //restful风格
    public String addStudent(TbStudent student){
        //同步到user表
        SysUser sysUser = new SysUser();
        sysUser.setUserName(student.getUsername());
        sysUser.setPassWord(student.getPassword());
        sysUser.setRealName(student.getStudentName());
        sysUser.setUserPhone(student.getTel());
        sysUser.setUserId(Integer.valueOf(NumUtil.getNum(5)));
//        sysUser.setUserId(Integer.valueOf(student.getPersonId()));
        sysUser.setIsDelete(0);
        sysUser.setUserIdentity(2);//学生身份
        sysUserService.addUser(sysUser);

        student.setIsDelete(0);
        tbStudentService.addStudent(student);
        return "";
    }

    @ResponseBody
    @SysLoging("根据id查询学生信息")
    @RequestMapping(value = "/getStudentById",method = RequestMethod.GET)
    public TbStudent getStudentById(int id){
        return tbStudentService.getStudentById(id);
    }

    @ResponseBody
    @SysLoging("获取学生信息列表")
    @RequestMapping(value = "/getStudentList",method = RequestMethod.GET)
    PageBean getTbStudentList(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer size){
        return tbStudentService.getStudentList(page,size);
    }

    @ResponseBody
    @SysLoging("删除学生信息")
    @RequestMapping(value = "/deleteStudent",method = RequestMethod.DELETE)
    public String deleteTbStudent(TbStudent Student) {
        TbStudent Student1 = tbStudentService.getStudentById(Student.getId());
        Student1.setIsDelete(1);
        tbStudentService.updateStudentById(Student1);
        return "";
    }

    @ResponseBody
    @SysLoging("更新学生信息")
    @RequestMapping(value = "/updateStudent",method = RequestMethod.PUT)
    public String updateStudent(TbStudent Student) {
        Student.setIsDelete(0);
        tbStudentService.updateStudentById(Student);
        return "";
    }
}
